<?php


namespace App\Http\Controllers\Api\Repositories;

use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxTag as Model;

class WxTagsRepositories
{
    /**
     * 添加
     * @param $tags_name
     * @return bool
     */
    public static function add($tags_name)
    {

        $tagModel = new Model();
        $tag = $tagModel->where('tags_name', $tags_name)->first();

        if ($tag) {
            return $tag;
        }

        $tagModel->tags_name = $tags_name;
        $tagModel->tags_number = 0;
        $tagModel->save();

        return $tagModel->where('tags_name', $tags_name)->first();

    }

    /**
     * 推荐标签
     */
    public static function recommendTags()
    {
        $tags = (new Model())
            ->where('tags_state', 0)
            ->where('tags_number', '>', 1)
            ->orderBy('tags_number', 'desc')->get();
        $tags->map(function ($items) {
            $items->tags_number = WxPostsTag::where('tags_id', $items->id)->count();
        });
        return $tags;
    }
}
